var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hasData: false,
    middle_height: "100vh",
    detailShow: '',
    dd_style: "",
    mode_text: '全部结算方式',
    mode: '',
    priceMode_text: '应收和应付',
    priceMode: '',
    warehouse: '全部仓库',
    warehouseId: '',
    tab: 1,
    imgList: [],
    is_invoice:false
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName + "--运单核验" });

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 40,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseid) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseid
      });
    }
    if (e.shareMode) {
      that.setData({
        shareMode: 1
      })
    }
    that.loadData();
  },
  switchChange(e) {
    console.log(e)
    var that = this;
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    util.getData("/atapi/finance/finance/payableReceivableList", {
      CO_ID: app.globalData.CO_ID,
      mode: that.data.mode,
      warehouse: that.data.warehouseId,
      priceMode: that.data.priceMode,
      'page': that.data.page,
      'pageSize': that.data.pageSize
    }, function (res, error) {
      console.log(res);
      console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        that.setData({
          datalist: res.data.data,
          stats: res.data.stats
        });
      } else {
        that.setData({
          datalist: [],
          stats: 0
        })
      };
    });

  },
  showMonthlyStatement: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/index/web?url=' + url
    })
  },
  completeDealwith: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    var datalist = that.data.datalist, currentData = {};
    for (var i = 0; i < datalist.length; i++) {
      if (datalist[i].ID == id) {
        currentData = datalist[i];
        that.setData({
          currentData: currentData
        })
      }
    }
    var _otherfee = currentData.otherfee, otherfee = [];
    console.log(_otherfee)
    if (_otherfee) {
      _otherfee = String(_otherfee).split(',');
      console.log(_otherfee)
      for (var i = 0; i < _otherfee.length; i++) {
        if(_otherfee[i]){
          var _ol = String(_otherfee[i]).split('|');
          otherfee.push({ name: _ol[0], value: _ol[1] })
        }
      }
    }
    that.setData({
      otherfee: otherfee
    })
    that.setData({
      modalName: 'dealwithModal'
    })
  },
  dealwith: function () {
    var that = this;
    var is_invoice = that.data.is_invoice?1:0;
    var realPrice = that.data.currentData.payable - that.data.currentData.paid;
    if(!realPrice || realPrice == 0 || realPrice == '' ){
      wx.showToast({
        title: "请输入应付金额",
        image: '/images/wrong.png',
        duration: 3000
      });
       return false;
    }
    if(realPrice < 0 || realPrice > that.data.currentData.payable ){
      wx.showToast({
        title: "金额超过应付",
        image: '/images/wrong.png',
        duration: 3000
      });
       return false;
    }

    wx.showModal({
      title: '温馨提示',
      content: '确定交付出纳开始付款吗？', 
      success(res) {
        if (res.confirm) {
          that.setData({
            modalName: null
          })
          wx.showLoading({
            title: '提交中',
          })
          var data = {
            id: that.data.currentData.ID,
            realPrice:realPrice,
            pay_remark:that.data.pay_remark||'',
            is_invoice: is_invoice,
            is_dealwith:Number(realPrice) == (Number(that.data.currentData.payable) - Number(that.data.currentData.paid))?1:0
          };
          util.postData("/atapi/finance/finance/payableDealwith",data , function (res, error) {
            console.log(res);
            console.log(error)
            wx.hideLoading();
            if (res.data.success) {
              wx.showToast({
                title: res.data.data,
                icon: 'success',
                duration: 3000,
                success: function () {
                  setTimeout(function () {
                    that.loadData();
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }
      }
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  changeMonthKnot: function (e) {
    var that = this;
    var itemList = ['全部结算方式', '月结', '现结']
    wx.showActionSheet({
      itemList: itemList,
      success(res) {
        if (res.tapIndex == 0) {
          that.setData({
            mode_text: itemList[res.tapIndex],
            mode: ''
          })
        }
        if (res.tapIndex == 1) {
          that.setData({
            mode_text: itemList[res.tapIndex],
            mode: 'month'
          })
        }
        if (res.tapIndex == 2) {
          that.setData({
            mode_text: itemList[res.tapIndex],
            mode: 'cash'
          })
        }
        that.loadData();
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  changePriceType: function (e) {
    var that = this;
    var itemList = ['应收和应付', '应收', '应付']
    wx.showActionSheet({
      itemList: itemList,
      success(res) {
        if (res.tapIndex == 0) {
          that.setData({
            priceMode_text: itemList[res.tapIndex],
            priceMode: ''
          })
        }
        if (res.tapIndex == 1) {
          that.setData({
            priceMode_text: itemList[res.tapIndex],
            priceMode: 'income'
          })
        }
        if (res.tapIndex == 2) {
          that.setData({
            priceMode_text: itemList[res.tapIndex],
            priceMode: 'pay'
          })
        }
        that.loadData();
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id),
      item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  previewImage: function (e) {
    var that = this;
    var current = e.currentTarget.dataset.url
    var imgList = that.data.imgList;
    wx.previewImage({
      current: current,
      urls: imgList
    })
  },
  showDeliveryDetail: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/order/order?id=' + id
    })
  }
})
